package cn.com.twoke.game.server.handler;

import cn.com.twoke.game.common.entity.message.GameDataMessage;
import cn.com.twoke.game.common.handler.MessageHandler;
import cn.com.twoke.game.common.utils.MessageUtil;
import cn.com.twoke.game.server.cache.GameRoomCache;
import cn.com.twoke.game.server.cache.OnlineUserCache;
import cn.com.twoke.game.server.entity.GameRoom;

import java.io.IOException;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;

public class GameDataMessageHandler implements MessageHandler<GameDataMessage> {

    @Override
    public void handle(Selector selector, SocketChannel channel, GameDataMessage message) throws IOException {
        GameRoom room = GameRoomCache.getByRoomId(message.getRoomId());
        if (room != null) {
            MessageUtil.writeMessage(OnlineUserCache.CACHE.get(room.getRoomMasterId()), message);
            room.getPlayerIds().forEach(item -> {
                MessageUtil.writeMessage(OnlineUserCache.CACHE.get(item), message);
            });
        }
    }

}
